"""


    PAL Make Doc

    This remote script generate an html documentation 
    of the Live python module.

"""

        
# set the path to create the Doc directory (must exists!):
DOCPATH = 'C:\\tmp\\PAL_MakeDoc'

def create_instance(c_instance):
    """
    This is called when you set the "Control Surface to PAL_MakeDoc"
    It creates a dummy live control.
    
    """
    # We first create a basic controler so that output is redirected:
    import PAL
    import PAL.liveControl
    import PAL.tools
    pal = PAL.liveControl.LiveControl( c_instance, name='Tester' )
    # this is to create a RuntimeError: Can't call 'document' at this time
    # to avoid creating the doc while Live is loading with PAL_MakeDoc selected in
    # midi remote script.
    # if the script is selected after startup, this will pass:
    Live.Application.get_application()
    
    pal.show_message( 'Updating Docs. this might take a while...')
    print '>>>>>>>>>>>>>>>PATH WHY EMPTY !?!'
    import sys
    for p in sys.path:
        p+'\n'
    print 'OK\n'
    
    # Now we update Live the doc:
    PAL.tools.MakeDoc( 'Live', DOCPATH )
    
    # And every midi remote script doc
    import os
    MRS_DOCPATH = os.path.join(DOCPATH,'MidiRemoteScripts')
    if not os.path.isdir( MRS_DOCPATH ):
        os.mkdir( MRS_DOCPATH )
    toDocPath = PAL.pal_config.LIVE_SCRIPT_PATH
    files = os.listdir( toDocPath ) 
    for f in files:
        if os.path.isdir(os.path.join(toDocPath,f)) and not f.startswith('PAL'):
            PAL.tools.MakeDoc( f, MRS_DOCPATH )

    # And return the controler so that Live is happy:
    pal.show_message( 'Docs Updated !')
    return pal

def Test():
    """
    Test the PAL_MakeDoc module using a mocked Live.
    (Generating the doc of the mocked Live :p)
    
    """
    print "Generating doc for LiveMock..."
    import sys,os
    sys.path.insert(0,os.path.join('.','..'))
    #print '>>>', sys.path
    import PAL.tools
    
    from PAL import LiveMock
    PAL.tools.MakeDoc( 'LiveMock', DOCPATH )
    print "Ok."

if __name__ == "__main__":
    """
    This is called only when you run this file in a python interpreter.
    I use it to validate code syntaxe and run tests.
    """
    Test()
